package com.feng.show.service.impl;

import com.feng.show.comm.ServerResponse;
import com.feng.show.dao.MerchantMapper;
import com.feng.show.dao.ProductAreaMapper;
import com.feng.show.dao.ProductTypeMapper;
import com.feng.show.dao.SecurityCodeMapper;
import com.feng.show.entity.*;
import com.feng.show.service.ISecurityCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("ISecurityCodeService")
public class SecurityCodeServiceImpl implements ISecurityCodeService {

    @Autowired
    private SecurityCodeMapper mapper;

    @Autowired
    private ProductAreaMapper areaMapper;

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    private RfidExample rfidExample;

    Date dateItem;

    @Transactional
    @Override
    public ServerResponse<String> bindSecurityCode(String userId, List<String> numberList, String productId, String merchantId) {

        dateItem = null;
        if (dateItem == null) {
            dateItem = new Date();
        }

        //

//        String number0=numberList.get(0);
//        SecurityCode securityCode=mapper.selectByPrimaryKey(number0);
//        if(securityCode!=null){
//            return ServerResponse.createByErrorMsg("数据已存在，请勿重复上传");
//        }

        int sum = 0;

        int sumFail=0;
        int listLen=numberList.size();
        for (int i = 0; i < numberList.size(); i++) {
            String number = numberList.get(i);

            //查询每一条数据，如果存在就不上传，不存在就上传
            SecurityCode securityCode = mapper.selectByPrimaryKey(number);


          //  SecurityCodeMapper
//            SecurityCodeExample example=new SecurityCodeExample();
//            SecurityCodeExample.Criteria criteria= example.createCriteria();
            //criteria.and
            if (securityCode != null) {
               // return ServerResponse.createByErrorMsg("数据已存在，请勿重复上传");
                ++sumFail;
                continue;
            }

            SecurityCode sc = new SecurityCode();
            //给地区id 一个默认值
            sc.setAreaId(Integer.valueOf("10001"));
            sc.setMerchantId(Integer.valueOf(merchantId));
            sc.setIncreateTime(dateItem);
            sc.setProductId(Integer.valueOf(productId));

            sc.setNumber(number);
            int status = 0;


            try {
                status = mapper.insert(sc);
            } catch (Exception e) {
                e.printStackTrace();
                return ServerResponse.createByErrorMsg("插入数据失败");
            }
            if (status == 1) {
                sum++;
            }
        }

        if(sumFail>0){
            if(sumFail==listLen){
                return ServerResponse.createBySuccessMsg("所有数据已存，上传失败");
            }else{
                return ServerResponse.createBySuccessMsg(sumFail+"条数据重复");
            }

        }
        if (sum > 0) {
            return ServerResponse.createBySuccessMsg("上传成功");
        } else {
            return ServerResponse.createByErrorMsg("上传失败");
        }

    }

    @Override
    public ServerResponse<String> bindSecurityCode(String userId, String number) {

        if (number == null) {
            return ServerResponse.createByErrorMsg("参数错误");
        }

        int count = mapper.deleteByPrimaryKey(number);
        if (count > 0) {
            return ServerResponse.createBySuccessMsg("上传成功");
        } else {
            return ServerResponse.createByErrorMsg("上传失败");
        }

    }

    @Override
    public ServerResponse<SecurityInfo> search(String userId, String number) {

        SecurityCode securityCode = mapper.selectByPrimaryKey(number);
        ProductArea productArea;
        ProductType productType;
        if (securityCode == null) {
            return ServerResponse.createByErrorMsg("商品不存在");
        } else {
            Integer areaId = securityCode.getAreaId();
            Integer typeId = securityCode.getProductId();
            Integer merchantId = securityCode.getMerchantId();

            SecurityInfo securityInfo = new SecurityInfo();

            if (areaId != null) {
                productArea = areaMapper.selectByPrimaryKey(typeId);
                if (productArea != null) {
                    securityInfo.setProductArea(productArea);
                }
            }
            if (typeId != null) {
                productType = productTypeMapper.selectByPrimaryKey(typeId);
                if (productType != null) {
                    securityInfo.setProductType(productType);
                }

            }
            if (merchantId != null) {
                Merchant merchant = merchantMapper.selectByPrimaryKey(merchantId);
                if (merchant != null) {
                    securityInfo.setMerchant(merchant);
                }

            }
            securityInfo.setSecurityCode(securityCode);
            return ServerResponse.createBySuccesMsg(securityInfo);

        }

    }
}
